local jinzuo = fk.CreateSkill {
  name = "th_jinzuo",
  max_turn_use_time = function(self, player)
    return player:getLostHp()
  end
}

jinzuo:addEffect(fk.EventPhaseChanging, {
  anim_type = "offensive",
  times = function(self, player)
    return Fk:currentRoom():getCurrent() == player and jinzuo:getRemainUseTime(player, Player.HistoryTurn) or -1
  end,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(jinzuo.name) and not data.skipped then
      if table.contains({ Player.Draw, Player.Play }, data.phase) then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local phase = Fk:translate(Util.PhaseStrMapper(data.phase))
    local to = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(false),
      min_num = 1,
      max_num = 1,
      prompt = "#th_jinzuo-invoke:::"..phase,
      skill_name = jinzuo.name,
      cancelable = true
    })
    if #to == 1 then
      local names = {}
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if not card.is_derived and not card.multiple_targets and card.is_damage_card then
          table.insertIfNeed(names, card.name)
        end
      end
      if #names ~= 0 then
        local user = to[1]
        local dat = room:askToUseVirtualCard(user, {
          name = names,
          skill_name = jinzuo.name,
          prompt = "#th_jinzuo-viewas",
          cancelable = false,
          extra_data = {
            bypass_distances = false,
            bypass_times = true,
          },
          skip = true,
        })
        if dat then
          event:setCostData(self, dat)
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use = event:getCostData(self)
    player:skip(data.phase)
    data.skipped = true
    use.extra_data = use.extra_data or {}
    use.extra_data.th_jinzuoAuthor = player
    room:useCard(use)
  end
}, { check_skill_limit = true })

jinzuo:addEffect(fk.Damage, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if data.card and data.damage > 0 then
      local parentUseData = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if parentUseData then
        local cardUseEvent = parentUseData.data
        return cardUseEvent.extra_data and cardUseEvent.extra_data.th_jinzuoAuthor == player
      end
    end
  end,
  on_cost =function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    if player:getMark("#th_jinzuo_draw-turn") == 0 then
      table.insert(choices, "#th_jinzuo_draw")
    end
    if player:canSkip(Player.Discard) and player:getMark("#th_jinzuo_phase-turn") == 0 then
      table.insert(choices, "#th_jinzuo_phase")
    end
    if #choices == 0 then return end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = jinzuo.name
    })
    event:setCostData(self, choice)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self)
    if choice == "#th_jinzuo_draw" then
      player:drawCards(3, jinzuo.name)
      room:setPlayerMark(player, "#th_jinzuo_draw-turn", 1)
    else
      player:skip(Player.Discard)
      room:setPlayerMark(player, "#th_jinzuo_phase-turn", 1)
    end
  end
})

Fk:loadTranslationTable{
  ["th_jinzuo"] = "经作",
  [":th_jinzuo"] = "每回合限X次，你可以跳过摸牌或出牌阶段并令一名角色视为使用一张单目标伤害牌（X为你已损失的体力值），此牌造成伤害后，你摸三张牌或跳过弃牌阶段（每回合每项限一次）。",

  ["#th_jinzuo-invoke"] = "经作：你可以跳过 %arg ，令一名角色视为使用一张单目标伤害牌",
  ["#th_jinzuo-viewas"] = "经作：你须视为使用一张单目标伤害牌",
  ["#th_jinzuo_draw"] = "摸三张牌",
  ["#th_jinzuo_phase"] = "跳过弃牌阶段",
}

return jinzuo